package cn.david.product.controller;

import cn.david.basic.exception.BusinessExcepiton;
import cn.david.basic.util.AjaxResult;
import cn.david.basic.util.PageList;
import cn.david.product.domain.Product;
import cn.david.product.domain.ProductDetail;
import cn.david.product.query.ProductQuery;
import cn.david.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productService;

    /**
     * 查询一个  localhost:8080/product/findone  1
     * localhost:8080/product/1
     * @return
     */
    @GetMapping("/detail/{id}")
    public ProductDetail findDetailByProductId(@PathVariable("id") Long id){
        ProductDetail detail = productService.findDetailByProductId(id);
        return detail;
    }

    /**
     * 查询所有localhost:8080/product
     * @return
     */
    @GetMapping

    public List<Product> findAll(){
        List<Product> list = productService.getAll();
        return list;
    }
    @PutMapping  /*@RequestBody  接收请求体localhost:8080/product*/
    public AjaxResult addorUpdate(@RequestBody Product product){
        try {
            if (product.getId()==null){//添加
                productService.add(product);
            }else{//修改
                productService.upadte(product);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败");
        }
    }

    @DeleteMapping("/{id}")  /*@RequestBody  接收请求体localhost:8080/product/4*/

    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            productService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败");
        }
    }

    @PostMapping
    public PageList<Product> queryForPage(@RequestBody ProductQuery productQuery){

        PageList<Product> pageList = productService.queryData(productQuery);
        return pageList;
    }
    @PostMapping("/list")
    public PageList<Product> queryList(@RequestBody ProductQuery productQuery){
        productQuery.setState(1);
        PageList<Product> pageList = productService.queryData(productQuery);
        return pageList;
    }




    @PatchMapping("/onSale")
    public AjaxResult onSale(@RequestBody Long[] ids){
        /*链式编程思维*/
        try {
            productService.onSale(ids);
            return AjaxResult.me();
        } catch (BusinessExcepiton e){
            return AjaxResult.me().setMessage(e.getMessage());

        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage("操作失败！"+e.getMessage());
        }
    }
    @PatchMapping("/fnSale")
    public AjaxResult fnSale(@RequestBody Long[] ids){
        /*链式编程思维*/
        try {
            productService.fnSale(ids);
            return AjaxResult.me();
        } catch (BusinessExcepiton e){
            return AjaxResult.me().setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage("操作失败！"+e.getMessage());
        }
    }
    @GetMapping("/{id}")
    public Product getDeail(@PathVariable("id") Long id){
        return productService.getById(id);

    }
}
